﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class CompanyService:BaseService,ICompanyService
    {
        #region prop and ctor
        [Autowired]
        private  ICompanyRepository companyRepository { get; set; }
        [Autowired]
        private IRegionService regionService { get; set; }  
        [Autowired]
        private  IMapper mapper { get;set; }
        public CompanyService()
        {
        }
        #endregion

        #region Method Servcie
        public async Task<BaseMessage> Add(AddCompany Company,int ID)
        {
            #region params verify  
            var verifyResualt = Company.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);
            }
            #endregion

            CompanyEntity entity = mapper.Map<CompanyEntity>(Company);
            await companyRepository.Create(entity,ID);
            if (await companyRepository.SaveChangesAsync())
            {
                return Ok200("添加成功!");
            }
            return BadRequest400("添加失败!");
        }
        public async Task<BaseMessage> Update(UpdateCompany Company,int UserID)
        {

            #region params verify  
            var verifyResualt = Company.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);
            }
            #endregion


            CompanyEntity entity = await companyRepository.FirstOrDefaultAsync(c=>c.ID==Company.ID);
            if (entity==null)
            {
                return BadRequest400("修改失败,选择的机构不存在;");
            }
             mapper.Map(Company,entity);

            await companyRepository.Update(entity,UserID);
            if (await companyRepository.SaveChangesAsync())
            {
                return Ok200("修改成功!");
            }
            return BadRequest400("修改失败!");
        }

        public async Task<DataMessage<CompanyView>> GetCompanys(CompanyParameter Params)
        {
            var query = companyRepository.GetCompanies(c=>true);
            return Ok200("查询成功!",await PageList<CompanyView>.CreateAsync(query,Params));
        }

        public async Task<BaseMessage> GetCompanyList(CompanyParameter Params)
        {
            var query = companyRepository.GetCompanyList(c => true);
            if (!string.IsNullOrEmpty(Params.Name))
            {
                query = query.Where(c=>c.DisplayName.Contains(Params.Name));
            }
            return Ok200("查询成功!",await query.ToListAsync());
        }

        public async Task<BaseMessage> Delete(int id,int UserID)
        {

            if (await companyRepository._dbContext.Files.Where(t=>t.CompanyID==id).AnyAsync())
            {
                return BadRequest400("已上传文件绑定该机构，请先删除文件。");
            }

            if (await companyRepository._dbContext.Users.Where(t => t.CompanyID == id).AnyAsync())
            {
                return BadRequest400("已有用户绑定该机构，不能删除。");
            }

            await companyRepository.Delete(t => t.ID == id);
            if(await companyRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }

        public async Task<DataMessage> GetCitysByCompany(int Id)
        {
            var query = companyRepository.GetCompanies(c=>c.ID==Id);
            IQueryable<PieView> data=null;
            if (await query.AnyAsync())
            {
             data=query.GroupBy(r => new { r.CityID, r.CityName }).Select(r=>new PieView() { Name=r.Key.CityName,Value=r.Key.CityID}) ;
             return Ok200("查询成功!", await data.ToListAsync());
            }
            return Ok200("查询成功!",null);
        }

        public async Task<BaseMessage> GetAreasByCompany(int Id)
        {

            var qcompany = companyRepository.GetCompanies(t=>t.ID==Id);
            RegionParameter rParams = new RegionParameter();
            if (await qcompany.AnyAsync())
            {
                var company=await qcompany.FirstOrDefaultAsync();
                if (company.PlaceTypeName.Contains("市"))
                {
                    rParams.ParentId = company.CityID;
                }
                else
                {
                    rParams.Id = company.AreaID;
                }
            }
            return await  regionService.RegionsAsync(rParams);
            //return Ok200("查询成功!", await query.ToListAsync());
        }
        #endregion
    }
}
